package com.zhkj.lib.base

import android.app.ActivityManager
import android.app.Application
import android.content.Context

abstract class BaseApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        INSTANCE = this

        prepareOnAllThread()
        if (isMainThread()) {
            prepareOnMainThread()
        }
    }

    companion object {
        lateinit var INSTANCE: Application
    }

    private fun isMainThread(): Boolean {
        val pid = android.os.Process.myPid();
        val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        for (appProcess in activityManager.runningAppProcesses) {
            if (appProcess.pid == pid) return applicationInfo.packageName == appProcess.processName
        }
        return false
    }

    abstract fun prepareOnMainThread()

    abstract fun prepareOnAllThread()
}